package com.example.dao_test;

import com.example.bean.Student11;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.List;

public class StudentTest01 {
    /*
        删除功能
     */
    @Test
    public void delete() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession(true);

        //执行对象
        int i = sqlSession.insert("StudentMapper.delete", 4);

        //获取执行结果
        System.out.println(i);
        //
        sqlSession.close();
        is.close();
    }

    /*
        修改功能
     */
    @Test
    public void update() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession(true);

        //执行对象
        Student11 student = new Student11(3, "name3", 33);
        int i = sqlSession.insert("StudentMapper.update", student);

        //获取执行结果
        System.out.println(i);
        //
        sqlSession.close();
        is.close();
    }

    /*
        新增功能
     */
    @Test
    public void insert() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession(true);

        //执行对象
        Student11 student = new Student11(4, "name4", 44);
        int i = sqlSession.insert("StudentMapper.insert", student);

        //获取执行结果
        System.out.println(i);
        //
        sqlSession.close();
        is.close();
    }

    /*
        根据id查询
     */
    @Test
    public void selectById() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();
        //执行对象
        Student11 o = sqlSession.selectOne("StudentMapper.selectById", 1);
        System.out.println(o);
        is.close();
        sqlSession.close();
    }

    /*
      查询全部
   */
    @Test
    public void selectAll() throws Exception {
        //    加载配置文件,Classloader也行,ServeltContext().getRealPath(),path=sr开始也行
        // InputStream is = StudentTest01.class.getClassLoader().getResourceAsStream("MyBatisConfig.xml");
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //    构建器SqlSessionFactoryBuilder

        //SqlSessionFactory对象

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //默认是手动提交事务,autoCommit=false
        //    SqlSession  对象
        SqlSession sqlSession = factory.openSession();

        //执行
        //类的全限定名.方法名,这个需要做的很好,动态代理就很好实现了
        List<Student11> students = sqlSession.selectList("StudentMapper.selectAll");
        //5.处理结果
        for (Student11 stu : students) {
            System.out.println(stu);
        }

        //释放资源
        is.close();
        sqlSession.close();
        //返回结果
    }
}
